/**
 * @file router 配置文件
 *
 * @author liubin29(liubin29@baidu.com)
 * 2015年10月27日
 */

'use strict';
import Vue from 'vue';
import Vuex from 'vuex';
import VueRouter from 'vue-router';
import ElementUI from 'element-ui';
import ElementUIPatch from 'common/element-ui-patch/index.js';
import 'element-ui/lib/theme-default/index.css';
import {cookie} from './common/util';
import App from './App.vue';
import store from './vuex/store';

Vue.use(Vuex);
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(ElementUIPatch);

Vue.config.debug = true;
const router = new VueRouter({
    saveScrollPosition: true,
    routes: [{
        path: '/',
        component: resolve => require(['./main/article-mng.vue'], resolve)
    }, {
        path: '/login',
        component: resolve => require(['./main/login.vue'], resolve)
    }, {
        path: '/column',
        component: resolve => require(['./main/column.vue'], resolve)
    }, {
        path: '/column-mng',
        component: resolve => require(['./main/column-mng.vue'], resolve)
    }, {
        path: '/article',
        component: resolve => require(['./main/article.vue'], resolve)
    }, {
        path: '/article-mng',
        component: resolve => require(['./main/article-mng.vue'], resolve)
    }, {
        name: 'media',
        path: '/pic',
        component: resolve => require(['./main/pic.vue'], resolve)
    }, {
        path: '/setting-index',
        component: resolve => require(['./main/setting-index.vue'], resolve)
    }, {
        path: '*',
        component: resolve => require(['./main/error.vue'], resolve)
    }]
});
router.beforeEach((to, from, next) => {
    router.app.$store.commit('setCurrentRoute', to.path);
    if (to.path !== '/login' && !cookie.get('userid')) {
        next('/login');
    }
    else {
        next();
    }
});
new Vue({
    el: '#app',
    render: h => h(App),
    router,
    store
});
